今天我們再來討論另一組關於正則表達式regular expressions
運用sub()可以替換掉特定的字母
設定的模式可以將sub(pattern = regex, replacement = string, x = string)帶入
透過這個函式我們可以找出其名稱帶有的字母並將字母替換掉想要的字母
假設這邊村長不小心把資料輸錯了,將帶有l的字母打成了i
race <- c("Elf", "Gobiin", "Harpy", "Hobiin", "Orc")
race
[1] "Elf" "Gobiin" "Harpy" "Hobiin" "Orc"
sub(pattern = "i",replacement = "l", x = race)
[1] "Elf" "Goblin" "Harpy" "Hoblin" "Orc"
有注意到這邊原先有兩個i但R這邊只會將第一個i改成l
因此若我們需要把所有的字母都改成l的話我們就需要用到gsub()
race
[1] "Elf" "Gobiin" "Harpy" "Hobiin" "Orc"
gsub(pattern = "i",replacement = "l", x = race)
[1] "Elf" "Goblln" "Harpy" "Hoblln" "Orc"
當然聽說最近村長想舉辦個猜字遊戲,我們同樣也可以用sub或gsub來用符號代替
|這個符號代表and也就是可以替換掉多個字母
race <- c("Elf", "Gobiin", "Harpy", "Hobiin", "Orc")
race
[1] "Elf" "Gobiin" "Harpy" "Hobiin" "Orc"
sub(pattern = "i|p",replacement = "_", x = race)
[1] "Elf" "Gob_in" "Har_y" "Hob_in" "Orc"
gsub(pattern = "i|p",replacement = "_", x = race)
[1] "Elf" "Gob__n" "Har_y" "Hob__n" "Orc"
就如前面提到的regular expression並非R專屬,這邊僅給予一些概念
未來還會持續努力